<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/qmake-manual.qdoc -->
<head>
  <title>Qt 4.3: Using Precompiled Headers</title>
  <link rel="prev" href="qmake-advanced-usage.html" />
  <link rel="contents" href="qmake-manual.html" />
  <link rel="next" href="qmake-reference.html" />
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><p>
[Previous: <a href="qmake-advanced-usage.html">qmake Advanced Usage</a>]
[<a href="qmake-manual.html">Contents</a>]
[Next: <a href="qmake-reference.html">qmake Reference</a>]
</p>
<h1 align="center">Using Precompiled Headers<br /><small></small></h1>
<a name="introduction"></a><p>Precompiled headers are a performance feature supported by some compilers to compile a stable body of code, and store the compiled state of the code in a binary file. During subsequent compilations, the compiler will load the stored state, and continue compiling the specified file. Each subsequent compilation is faster because the stable code does not need to be recompiled.</p>
<p><tt>qmake</tt> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
<ul>
<li>Windows<ul>
<li>nmake</li>
<li>Dsp projects (VC 6.0)</li>
<li>Vcproj projects (VC 7.0 &amp; 7.1)</li>
</ul>
</li>
<li>Mac OS X<ul>
<li>Makefile</li>
<li>Xcode</li>
</ul>
</li>
<li>Unix<ul>
<li>GCC 3.4 and above</li>
</ul>
</li>
</ul>
<a name="add-pch"></a><a name="adding-precompiled-headers-to-your-project"></a>
<h2>Adding Precompiled Headers to Your Project</h2>
<a name="pch-contents"></a><a name="contents-of-the-precompiled-header-file"></a>
<h3>Contents of the Precompiled Header File</h3>
<p>The precompiled header must contain code which is <i>stable</i> and <i>static</i> throughout your project. A typical PCH might look like this:</p>
<a name="example"></a>
<h4>Example: <tt>stable.h</tt></h4>
<pre><span class="comment"> //</span> Add C includes here

 #if defined __cplusplus
<span class="comment"> //</span> Add C++ includes here
 #include &lt;stdlib&gt;
 #include &lt;iostream&gt;
 #include &lt;vector&gt;
 #include &lt;QApplication&gt; <span class="comment">//</span> Qt includes
 #include &lt;QPushButton&gt;
 #include &lt;QLabel&gt;
 #include &quot;thirdparty/include/libmain.h&quot;
 #include &quot;my_stable_class.h&quot;
 ...
 #endif</pre>
<p>Note that a precompiled header file needs to separate C includes from C++ includes, since the precompiled header file for C files may not contain C++ code.</p>
<a name="project-options"></a><a name="project-options"></a>
<h3>Project Options</h3>
<p>To make your project use PCH, you only need to define the <tt>PRECOMPILED_HEADER</tt> variable in your project file:</p>
<pre>     PRECOMPILED_HEADER = stable.h</pre>
<p><tt>qmake</tt> will handle the rest, to ensure the creation and use of the precompiled header file. You do not need to include the precompiled header file in <tt>HEADERS</tt>, as <tt>qmake</tt> will do this if the configuration supports PCH.</p>
<p>All platforms that support precompiled headers have the configuration option <tt>precompile_header</tt> set. Using this option, you may trigger conditional blocks in your project file to add settings when using PCH. For example:</p>
<pre>     precompile_header:!isEmpty(PRECOMPILED_HEADER) {
     DEFINES += USING_PCH
     }</pre>
<a name="notes-on-possible-issues"></a>
<h2>Notes on Possible Issues</h2>
<p>On some platforms, the file name suffix for precompiled header files is the same as that for other object files. For example, the following declarations may cause two different object files with the same name to be generated:</p>
<pre>     PRECOMPILED_HEADER = window.h
     SOURCES            = window.cpp</pre>
<p>To avoid potential conflicts like these, it is good practice to ensure that header files that will be precompiled are given distinctive names.</p>
<a name="example-project"></a><a name="example-project"></a>
<h2>Example Project</h2>
<p>You can find the following source code in the <tt>examples/qmake/precompile</tt> directory in the Qt distribution:</p>
<a name=""></a>
<h3><tt>mydialog.ui</tt></h3>
<pre> &lt;ui version=&quot;4.0&quot; &gt;
  &lt;author&gt;&lt;/author&gt;
  &lt;comment&gt;&lt;/comment&gt;
  &lt;exportmacro&gt;&lt;/exportmacro&gt;
  &lt;class&gt;MyDialog&lt;/class&gt;
  &lt;widget class=&quot;QDialog&quot; name=&quot;MyDialog&quot; &gt;
   &lt;property name=&quot;geometry&quot; &gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;401&lt;/width&gt;
     &lt;height&gt;70&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;property name=&quot;windowTitle&quot; &gt;
    &lt;string&gt;Mach 2!&lt;/string&gt;
   &lt;/property&gt;
   &lt;layout class=&quot;QVBoxLayout&quot; &gt;
    &lt;property name=&quot;margin&quot; &gt;
     &lt;number&gt;9&lt;/number&gt;
    &lt;/property&gt;
    &lt;property name=&quot;spacing&quot; &gt;
     &lt;number&gt;6&lt;/number&gt;
    &lt;/property&gt;
    &lt;item&gt;
     &lt;widget class=&quot;QLabel&quot; name=&quot;aLabel&quot; &gt;
      &lt;property name=&quot;text&quot; &gt;
       &lt;string&gt;Join the life in the fastlane; - PCH enable your project today! -&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/item&gt;
    &lt;item&gt;
     &lt;widget class=&quot;QPushButton&quot; name=&quot;aButton&quot; &gt;
      &lt;property name=&quot;text&quot; &gt;
       &lt;string&gt;&amp;amp;Quit&lt;/string&gt;
      &lt;/property&gt;
      &lt;property name=&quot;shortcut&quot; &gt;
       &lt;string&gt;Alt+Q&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/item&gt;
   &lt;/layout&gt;
  &lt;/widget&gt;
  &lt;pixmapfunction&gt;qPixmapFromMimeSource&lt;/pixmapfunction&gt;
  &lt;resources/&gt;
  &lt;connections/&gt;
 &lt;/ui&gt;</pre>
<a name=""></a>
<h3><tt>stable.h</tt></h3>
<pre><span class="comment"> /* Add C includes here */</span>

 #if defined __cplusplus
<span class="comment"> /* Add C++ includes here */</span>

 # include &lt;iostream&gt;
 # include &lt;QApplication&gt;
 # include &lt;QPushButton&gt;
 # include &lt;QLabel&gt;
 #endif</pre>
<a name=""></a>
<h3><tt>myobject.h</tt></h3>
<pre> #include &lt;QObject&gt;

 class MyObject : public QObject
 {
 public:
     MyObject();
     ~MyObject();
 };</pre>
<a name=""></a>
<h3><tt>myobject.cpp</tt></h3>
<pre> #include &lt;iostream&gt;
 #include &lt;QDebug&gt;
 #include &lt;QObject&gt;
 #include &quot;myobject.h&quot;

 MyObject::MyObject()
     : QObject()
 {
     std::cout &lt;&lt; &quot;MyObject::MyObject()\n&quot;;
 }</pre>
<a name=""></a>
<h3><tt>util.cpp</tt></h3>
<pre> void util_function_does_nothing()
 {
     <span class="comment">//</span> Nothing here...
     int x = 0;
     ++x;
 }</pre>
<a name=""></a>
<h3><tt>main.cpp</tt></h3>
<pre> #include &lt;QApplication&gt;
 #include &lt;QPushButton&gt;
 #include &lt;QLabel&gt;
 #include &quot;myobject.h&quot;
 #include &quot;mydialog.h&quot;

 int main(int argc, char **argv)
 {
     QApplication app(argc, argv);

     MyObject obj;
     MyDialog dialog;

     dialog.connect(dialog.aButton, SIGNAL(clicked()), SLOT(close()));
     dialog.show();

     return app.exec();
 }</pre>
<a name=""></a>
<h3><tt>precompile.pro</tt></h3>
<pre> #############################################
 #
 # Example for using Precompiled Headers
 #
 #############################################
 TEMPLATE  = app
 LANGUAGE  = C++
 CONFIG   += console precompile_header

 # Use Precompiled headers (PCH)
 PRECOMPILED_HEADER  = stable.h

 HEADERS   = stable.h \
             mydialog.h \
             myobject.h
 SOURCES   = main.cpp \
             mydialog.cpp \
             myobject.cpp \
             util.cpp
 FORMS     = mydialog.ui</pre>
<p>
[Previous: <a href="qmake-advanced-usage.html">qmake Advanced Usage</a>]
[<a href="qmake-manual.html">Contents</a>]
[Next: <a href="qmake-reference.html">qmake Reference</a>]
</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
